package com.wtwd.device.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.wtwd.device.dao.AppUserTargetMapper;
import com.wtwd.device.model.entity.AppUserTarget;
import com.wtwd.device.service.AppUserTargetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zdl
 * @since 2021-09-03
 */
@Service
public class AppUserTargetServiceImpl implements AppUserTargetService {
    @Resource
    private AppUserTargetMapper appUserTargetMapper;

    @Override
    public AppUserTarget getByUserId(Long userId) {
        return appUserTargetMapper.selectById(userId);
    }

    @Override
    public boolean save(AppUserTarget appUserTarget) {
        AppUserTarget user = getByUserId(appUserTarget.getUserId());
        int row;
        if (user == null) {
            AppUserTarget defaultObj = new AppUserTarget().getDefaultObj(appUserTarget.getUserId());
            BeanUtil.copyProperties(appUserTarget,defaultObj, CopyOptions.create().setIgnoreNullValue(true));
            row = appUserTargetMapper.insert(defaultObj);
        } else {
            row = appUserTargetMapper.updateById(appUserTarget);
        }
        return row == 1;
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        return appUserTargetMapper.deleteById(userId)==1;
    }

}
